package com.ruoyi.sycs.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjUtil;
import com.ruoyi.common.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sycs.domain.vo.BsRuleVo;
import com.ruoyi.sycs.domain.bo.BsRuleBo;
import com.ruoyi.sycs.service.IBsRuleService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 编号规则设置
 *
 * @author zyb
 * @date 2024-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sycs/bsRule")
public class BsRuleController extends BaseController {

    private final IBsRuleService iBsRuleService;

    /**
     * 查询编号规则设置列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<BsRuleVo> list(BsRuleBo bo, PageQuery pageQuery) {
        return iBsRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出编号规则设置列表
     */
    @SaCheckLogin
    @Log(title = "编号规则设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BsRuleBo bo, HttpServletResponse response) {
        List<BsRuleVo> list = iBsRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "编号规则设置", BsRuleVo.class, response);
    }

    /**
     * 获取编号规则设置详细信息
     *
     * @param ruleId 主键
     */
    @SaCheckLogin
    @GetMapping("/{ruleId}")
    public R<BsRuleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ruleId) {
        return R.ok(iBsRuleService.queryById(ruleId));
    }
    @SaCheckLogin
    @GetMapping("/getRule")
    public R<BsRuleVo> getRule(BsRuleBo bo) {
        return R.ok(iBsRuleService.getRule(bo));
    }

    /**
     * 新增编号规则设置
     */
    @SaCheckLogin
    @Log(title = "编号规则设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BsRuleBo bo) {
        return toAjax(iBsRuleService.insertByBo(bo));
    }

    /**
     * 修改编号规则设置
     */
    @SaCheckLogin
    @Log(title = "编号规则设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BsRuleBo bo) {
        return toAjax(iBsRuleService.updateByBo(bo));
    }

    /**
     * 删除编号规则设置
     *
     * @param ruleIds 主键串
     */
    @SaCheckLogin
    @Log(title = "编号规则设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ruleIds) {
        return toAjax(iBsRuleService.deleteWithValidByIds(Arrays.asList(ruleIds), true));
    }

    /**
     * 根据试验室和试验类型对应的编号规则示例
     * @param bo
     * @return
     */
    @SaCheckLogin
    @GetMapping("/getRuleSl")
    public BsRuleVo getRuleSl(BsRuleBo bo) {
        return iBsRuleService.getRuleSl(bo);
    }

    /**
     * 根据编号规则生成编号
     * @param bo
     * @return
     */
    @SaCheckLogin
    @GetMapping("/getRuleRes")
    public R<BsRuleVo> getRuleRes(BsRuleBo bo) {
        if (ObjUtil.isEmpty(bo.getSyTypeId())) {
            throw new ServiceException("生成编号规则，syTypeId不能为空");
        }
        if (ObjUtil.isEmpty(bo.getDeptId())) {
            throw new ServiceException("生成编号规则，deptId不能为空");
        }
        return R.ok(iBsRuleService.getRuleRes(bo));
    }
}
